﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class ServiceTermsListControl : YuanlinUserControl,
        IReplyConverter<ServiceTermsReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList serviceTerms;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetServiceTermsListRequest(this, pager.PageIndex, pager.PageSize));
        }
        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }

            list.DataSource = serviceTerms;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(serviceTerms.TotalCount);
                currentPage.TotalRecords = (serviceTerms.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteServiceTerms(int serviceTermsId)
        {
            BusinessManager.DeleteServiceTerms(serviceTermsId);
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteServiceTermss(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteServiceTerms(entityId);                
                }
            }
            return 0;
        }

        #endregion


        #region IReplyConverter<ServiceTermsReply,GetListOperation> Members

        public void ConvertReply(ServiceTermsReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                serviceTerms = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}